//
//  FavioutMvViewController.m
//  MusicPlayer
//
//  Created by dlios on 15-6-1.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FavioutMvViewController.h"
#import "MVmodel.h"
#import "SANetWorkingTask.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "MVTableViewCell.h"
#import <MediaPlayer/MediaPlayer.h>
@interface FavioutMvViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *array;

@end

@implementation FavioutMvViewController

- (void)dealloc
{
    //移除通知
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
    [_array release];
    [_table release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.array = [NSMutableArray array];
    }
    return self;
}

-(void)loadView
{
    [super loadView];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.title = @"热门MV";
    [self creatView];
    
    UIBarButtonItem *exits = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(exitsAction:)];
    self.navigationItem.leftBarButtonItem = exits;
    
    [SANetWorkingTask request:@"http://api.dongting.com/channel/channel/mvs?page=1&size=10" parmater:nil block:^(NSDictionary *result) {
        NSArray *array = [result objectForKey:@"data"];
        
        for (NSDictionary *list in array) {
            MVmodel *mv = [[MVmodel alloc] init];
            [mv setValuesForKeysWithDictionary:list];
            [self.array addObject:mv];
            [mv release];
            
        }
        
        [_table reloadData];
        
    }];
    
    
    
    
}

#pragma mark - 返回图片的事件
- (void)exitsAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)creatView
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -self.view.frame.size.height/667* 64 - self.view.frame.size.height/667*49 -self.view.frame.size.height/667*25 ) style:UITableViewStylePlain];
    
    _table.rowHeight = self.view.frame.size.height/667*280;
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    [_table release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cell";
    MVTableViewCell    *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[[MVTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease
                ];
    }
    
    MVmodel *model = [self.array objectAtIndex:indexPath.row];
    cell.introLabel.text = model.desc ;
    
    NSArray *arr = [model.title componentsSeparatedByString:@"/"];
    cell.timaLabel.text = [arr objectAtIndex:0];
    cell.timaLabe1.text = [arr objectAtIndex:1];
    
    NSDictionary *dic = [model.tag objectAtIndex:0];
    
    cell.tag1.text = [dic objectForKey:@"tagName"];
    cell.tag1.backgroundColor = [UIColor colorWithRed:226/255.0 green:186/255.0 blue:191/255.0 alpha:1];
    
    if (model.tag.count  == 2) {
        
        NSDictionary *dic2 = [model.tag objectAtIndex:1];
        cell.tag2.text = [dic2 objectForKey:@"tagName"];
        cell.tag2.backgroundColor = [UIColor colorWithRed:247/255.0 green:205/255.0 blue:136/255.0 alpha:1];
    }
    
    
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.bigPicUrl]];
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playingDone) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
    MVmodel *model = [self.array objectAtIndex:indexPath.row];
    
    NSDictionary *dic1 = [model.mvList objectAtIndex:0];
    
    NSString *str = [dic1 objectForKey:@"url"];
    moviePlayeer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:str]];
    moviePlayeer.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
    
    moviePlayeer.moviePlayer.scalingMode = MPMovieScalingModeNone;
    
    [[[UIApplication sharedApplication]keyWindow]addSubview:moviePlayeer.view];

    
    
}



-(void)playingDone

{
    
    NSLog(@"播放完成");
    
    [moviePlayeer.view removeFromSuperview];
    
    moviePlayeer = nil;
    
}





@end
